<template>
	<view class="w-1 p-30">
		<view class="bg-ff br-20">
			<view class="fr-s-c bb-solid p-30" @click="chooseImage">
				<text class="c-33 fs-30 bold flex-1">头像</text>
				<image class="circle" style="height: 92rpx; width: 92rpx;" :src="user.avatar || '/static/logo.png'"
					mode="aspectFill"></image>
				<up-icon name="arrow-right"></up-icon>
			</view>

			<view class="w-1 relative p-30 bb-solid fr-s-c" @click="shownick">
				<text class="c-33 bold fs-30" style="width: 176rpx;">昵称</text>
				<input v-model.trim="user.nickname" type="text" placeholder="请输入昵称" class="flex-1 t-r fs-28" />
				<up-icon name="arrow-right"></up-icon>
				<view class="absolute-0 w-1 h-1"></view>
			</view>

			<view class="w-1 relative p-30 bb-solid fr-s-c" @click="showact">
				<text class="c-33 bold fs-30" style="width: 176rpx;">性别</text>
				<input type="text" placeholder="请选择性别" :value="itemList[curs]" class="flex-1 t-r fs-28" />
				<up-icon name="arrow-right"></up-icon>
				<view class="w-1 h-1 absolute-0" style="z-index: 2;"></view>
			</view>

			<picker class="w-1 bb-solid" mode="date" :value="user.birthday" @change="changeDate">
				<view class="w-1 p-30 relative fr-s-c">
					<text class="c-33 bold fs-30" style="width: 176rpx;">出生日期</text>
					<input type="text" :value="user.birthday" placeholder="请选择出生日期" class="flex-1 t-r fs-28" />
					<up-icon name="arrow-right"></up-icon>
					<view class="w-1 h-1 absolute-0" style="z-index: 2;"></view>
				</view>
			</picker>

			<view class="w-1 relative p-30 bb-solid fr-s-c" @click="copy">
				<text class="c-33 bold fs-30" style="width: 176rpx;">邀请码</text>
				<input type="text" placeholder="邀请码" :value="user.invicode" class="flex-1 t-r fs-28" />
				<up-icon name="file-text-fill" size="40rpx"></up-icon>
				<view class="w-1 h-1 absolute-0" style="z-index: 2;"></view>
			</view>

			<view class="w-1 relative p-30 bb-solid fr-s-c">
				<text class="c-33 bold fs-30" style="width: 176rpx;">专属客户经理</text>
				<input type="text" placeholder="无" :value="user.manager" class="flex-1 t-r fs-28" />
				<!-- <up-icon name="file-text-fill" size="40rpx"></up-icon> -->
				<view class="w-1 h-1 absolute-0" style="z-index: 2;"></view>
			</view>

			<!-- if((type==0 && !user.value.yyzzimg) || (type==1 && !user.value.zcardimg)){ -->
			<block v-if="(user.yyzzimg && user.zcardimg)">
				<view class="w-1 relative p-30 bb-solid fr-s-c" @click="previewImage(0)">
					<text class="c-33 bold fs-30" style="width: 176rpx;">营业执照</text>
					<input type="text" :placeholder="user.yyzzimg?'点击查看':'点击上传'" class="flex-1 t-r fs-28" />
					<up-icon name="arrow-right"></up-icon>
					<view class="absolute-0 w-1 h-1" style="z-index: 9;"></view>
				</view>

				<view class="w-1 relative p-30 fr-s-c" @click="previewImage(1)">
					<text class="c-33 bold fs-30" style="width: 176rpx;">身份证照</text>
					<input type="text" :placeholder="user.zcardimg?'点击查看':'点击上传'" class="flex-1 t-r fs-28" />
					<up-icon name="arrow-right"></up-icon>
					<view class="absolute-0 w-1 h-1" style="z-index: 9;"></view>
				</view>
			</block>

			<view v-else class="w-1 relative p-30 fr-s-c" @click="previewImage(1)">
				<text class="c-33 bold fs-30" style="width: 176rpx;">完善认证信息</text>
				<input type="text" :placeholder="user.zcardimg?'点击查看':'点击上传'" class="flex-1 t-r fs-28" />
				<up-icon name="arrow-right"></up-icon>
				<view class="absolute-0 w-1 h-1" style="z-index: 9;"></view>
			</view>
		</view>


		<!-- <view class="iphonex">
			<view class="w-1 p-30 fr-c">
				<view class="big-btn fr-c" style="width: 690rpx;" @click="sub">确认修改</view>
			</view>
		</view> -->
	</view>
</template>

<script setup>
	const itemList = ['女', '男'];
	const curs = ref(-1);

	// function changeSex(index){
	// 	curs.value = index;

	// }

	function copy() {
		uni.setClipboardData({
			data: user.value.invicode
		})
	}
	onLoad(() => {
		uni.$post({
			url: "/api/Realauth/isRealAuth",
		}).then(res => {
			if (res.data.status == '2') {
				uni.$u.toast('实名认证审核未通过，请重新提交')
			}
		})
	})

	function shownick() {
		uni.showModal({
			title: '昵称',
			editable: true,
			placeholderText: user.value.nickname || '请输入昵称',
			success: (ret) => {
				if (ret.confirm) {
					if (!ret.content.trim().length) return uni.$u.toast('请输入昵称');
					uni.$post({
						url: '/api/user/Upmyinfo',
						data: {
							nickname: ret.content,
						},
					}).then(res => {
						user.value.nickname = ret.content;
					})
				}
			}
		})
	}


	function previewImage(type) {
		if ((type == 0 && !user.value.yyzzimg) || (type == 1 && !user.value.zcardimg)) {
			uni.navigateTo({
				url: '/pages/Login/FillInfo'
			})
		} else {
			uni.previewImage({
				current: 0,
				urls: type == 0 ? [uni.$dimg(user.value.yyzzimg)] : [uni.$dimg(user.value.zcardimg), uni.$dimg(user
					.value.bcardimg)]
			})
		}
	}

	function showact() {
		uni.showActionSheet({
			itemList: itemList,
			success: (ret) => {
				curs.value = ret.tapIndex
				uni.$post({
					url: '/api/user/Upmyinfo',
					data: {
						gender: ret.tapIndex,
					},
				}).then(res => {
					user.value.gender = ret.tapIndex;
				})
			}
		})
	}


	function changeDate(e) {
		console.log(e.detail.value)
		uni.$post({
			url: '/api/user/Upmyinfo',
			data: {
				birthday: e.detail.value,
			},
		}).then(res => {
			user.value.birthday = e.detail.value;
		})
	}

	const user = ref('')
	onShow(() => {
		getuser()
	})
	const getuser = () => {
		uni.$post({
			url: '/api/User/Userinfo',
			loading: false
		}).then(res => {
			for (let i in res.data) {
				if (res.data[i] == null) {
					res.data[i] = ''
				}
			}
			user.value = res.data;
			curs.value = res.data.gender;
		})
	}






	function chooseImage() {
		uni.chooseImage({
			count: 1, //默认9
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			success: async (res) => {
				const avatar = res.tempFilePaths
				const imageres = await uni.$up({
					imageUrl: avatar
				})
				uni.$post({
					url: '/api/user/Upmyinfo',
					data: {
						avatar: imageres[0].fullurl,
					}
				}).then(res => {
					user.value.avatar = imageres[0].fullurl
					uni.hideLoading()
				})

			}
		});
	}

	function sub() {
		if (!(user.value.nickname.trim().length)) return uni.$u.toast('请输入昵称');
		// if(!(user.value.realname.trim().length)) return uni.$u.toast('请输入姓名');
		uni.$post({
			url: '/api/user/Upmyinfo',
			data: {
				avatar: user.value.avatar,
				nickname: user.value.nickname,
				// realname:user.value.realname,
				gender: user.value.gender,
				birthday: user.value.birthday
			},
		}).then(res => {
			getApp().tt(res.msg, 'success')
			uni.$delayBack()
		})
	}
</script>

<style>
	page {
		background: #f1f5f6;
	}
</style>